package com.vic.designpattern.patterns.c_behavioral.state.demo2;

/**
 * 环境类角色
 * @author victor
 */
public class Account {

    // 维持一个对 抽象状态对象 的引用
    private AccountState state;

    /**
     * 用户名
     */
    private String owner;

    /**
     * 账户余额
     */
    private double balance = 0D;

    public Account(String owner, double init) {
        this.owner = owner;
        this.balance = init;
        // 设置初始状态
        this.state = new NormalState(this);
        System.out.println(this.owner + "开户，初始金额为" + init);
        System.out.println("------------------------------------------");
    }

    public AccountState getState() {
        return state;
    }

    public void setState(AccountState state) {
        this.state = state;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    // 存款
    public void deposit(double amount) {
        System.out.println(this.owner + "存款" + amount);
        // 调用状态对象 deposit 方法
        state.deposit(amount);
        System.out.println("现在余额为" + this.balance);
        System.out.println("现在账户状态为" + this.state.getClass().getSimpleName());
        System.out.println("------------------------------------------");
    }

    // 取款
    public void withdraw(double amount) {
        System.out.println(this.owner + "取款" + amount);
        // 调用状态对象 withdraw 方法
        state.withdraw(amount);
        System.out.println("现在余额为" + this.balance);
        System.out.println("现在账户状态为" + this.state.getClass().getSimpleName());
        System.out.println("------------------------------------------");
    }

    // 计算利息
    public void computeInterest() {
        // 调用状态对象 computeInterest 方法
        state.computeInterest();
    }
}
